package com.wuming.caiwu.util;


import com.baidu.aip.ocr.AipOcr;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * @author xuhuai
 * @createTime 2019/5/25 10:24
 * @Description:
 */
public class CertNoUtil {

    /**
     * 重要提示代码中所需工具类
     * FileUtil,Base64Util,HttpUtil,GsonUtils请从
     * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
     * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
     * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
     * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
     * 下载
     */
    public static void main(String[] args) {
        // 初始化一个AipOcr
        AipOcr client = new AipOcr("16340585", "hpPRzHjC7biZu60WHjXAia5G", "YMP5VGspNwpUH4NPPhz5eZ9DX37WSez9");
        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 调用接口
        String path = "D:\\1.png";
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("language_type", "CHN_ENG");
        JSONObject res = client.basicGeneral(path, options);
        System.out.println(res.toString());
    }

    public static JSONObject identifyCertNoInfos(String filePath) {
        // 初始化一个AipOcr
        AipOcr client = new AipOcr("16340585", "hpPRzHjC7biZu60WHjXAia5G", "YMP5VGspNwpUH4NPPhz5eZ9DX37WSez9");
        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("language_type", "CHN_ENG");
        JSONObject res = client.idcard(filePath, "front", options);
        String imageStatus = res.getString("image_status");
        JSONObject userInfo = new JSONObject();
        if ("normal".equalsIgnoreCase(imageStatus)) {
            JSONObject userInfoJson = res.getJSONObject("words_result");
            JSONObject nameJson = userInfoJson.getJSONObject("姓名");
            JSONObject addrJson = userInfoJson.getJSONObject("住址");
            JSONObject certNoJson = userInfoJson.getJSONObject("公民身份号码");
            JSONObject birthdayJson = userInfoJson.getJSONObject("出生");
            JSONObject sexJson = userInfoJson.getJSONObject("性别");
            userInfo.put("studentName", nameJson.getString("words"));
            userInfo.put("registerAddr", addrJson.getString("words"));
            userInfo.put("certNo", certNoJson.getString("words"));
            userInfo.put("registerAddr", addrJson.getString("words"));
            userInfo.put("birthday", birthdayJson.getString("words"));
            userInfo.put("sex", sexJson.getString("words").equalsIgnoreCase("男") ? 0 : 1);
        }
        return userInfo;
    }

}
